mini.parse();

function GradeRuleFormHandler() {
};

GradeRuleFormHandler.instance = new GradeRuleFormHandler();

//获得全局的规则表单对象
GradeRuleFormHandler.gradeRuleForm = new mini.Form("gradeRuleForm");
GradeRuleFormHandler.formData = {};

// 规则启用你状态数据
GradeRuleFormHandler.bgrStateData = [
	{text: "启用",value: 1},
	{text: "不启用",value: 0}
];
mini.get("bgrState").setData(GradeRuleFormHandler.bgrStateData);

//判断是否为新增等级规则：true-是，false-不是。
GradeRuleFormHandler.isAddNew = "";

//创建上传购物券图片控件
SWFUploadTools.createSWFUBtn(__realpath + __gradeIconPath, null, "bgrUpload", uploadBgrIconSuccess);

/**
 * 保存（添加或编辑）数据的方法
 */
GradeRuleFormHandler.save = function(){
	var gradeRuleData = GradeRuleFormHandler.gradeRuleForm.getData();
	
    GradeRuleFormHandler.gradeRuleForm.validate();
    if (GradeRuleFormHandler.gradeRuleForm.isValid() == false) return;
    
    var json = mini.encode(gradeRuleData);
    $.ajax({
        url: __root + "/saveOrUpdateGradeRule.do",
        type: "POST",
        data: {
        	gradeRuleInfo: json,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp,
    		isAddNew: GradeRuleFormHandler.isAddNew
        },
        cache: false,
        success: function (text) {
        	MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
        		GradeRuleFormHandler.closeCurrentWin("save");
        	});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示",jqXHR.responseText, function(){
        		GradeRuleFormHandler.closeCurrentWin();
        	});
        }
    });
    
}

/**
 * 标准方法接口定义
 * @param {} data
 */
GradeRuleFormHandler.passData = function(data) {
    if (data.action == "edit") {
        // 跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        
        // 根据id获取对应的规则信息
        $.ajax({ 
            url: __root + "/getGradeRuleById.do?bgrId=" + data.bgrId,
            cache: false,
            dataType: 'json',
            success: function (text) {
                GradeRuleFormHandler.gradeRuleForm.setData(text);
                
                if(text.bgrIcon != "" && text.bgrIcon.length > 0){
                	$("#imgViewer").empty();
					
					var iframe = "<iframe src='" + __root + "/imgViewerContorller.do?basePath=" + __realpath+__gradeIconPath + "&newImgPath=" + text.bgrIcon + "' frameborder='0' border='0' style='margin:0;padding:0;width:100%;height:100%;'/>";
					$("#imgViewer").append(iframe);
                }
                
                // 编辑等级规则时不可编辑状态
                mini.get("bgrState").disable();
            }
        });
        
        // 编辑
        GradeRuleFormHandler.isAddNew = false;
    }else{
    	// 跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        // 设置新增等级序列
    	mini.get("bgrId").setValue(data.newBgrId);
    	// 默认类型状态为有效
    	mini.get("bgrState").setValue(1);
    	// 新增
    	GradeRuleFormHandler.isAddNew = true;
    }
}

/**
 * 关闭窗口
 * @param {} action
 */
GradeRuleFormHandler.closeCurrentWin = function(action) {
    if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
    else window.close();
}

/**
 * 关闭窗口
 */
GradeRuleFormHandler.closeMiniWin = function(){
	GradeRuleFormHandler.closeCurrentWin("cancel");
}

/**
 * 处理上传成功后的图片
 * @param {} file
 * @param {} serverData
 */
function uploadBgrIconSuccess(file, serverData) {
	MiniuiTools.miniHideLoading();
	MiniuiTools.miniInfo("系统提示","文件上传成功！");
	
	$("#imgViewer").empty();
	var fdata = mini.decode(serverData);
	var newPath = fdata.imgPath;	
	var iframe = "<iframe src='" + __root + "/imgViewerContorller.do?basePath=" + __realpath+__gradeIconPath + "&newImgPath=" + newPath + "' frameborder='0' border='0' style='margin:0;padding:0;width:100%;height:100%;'/>";
	$("#imgViewer").append(iframe);
	
	mini.get("upload_bgrIcon").setValue(newPath);
}

/**
 * 等级升级规则表达式验证过程
 * @param {} e
 */
function validBgrUpRule(e){
	var bgrUprulesql = mini.get("bgrUprulesql");
	var expSql = "select (case when @@conditions then 1 end) into @exe_result";
	var expText = e.value.trim();
	
	if(/^$/.test(expText)){
		bgrUprulesql.setValue("");
		e.errorText = "请填写升级规则表达式！";
        e.isValid = false;
	}else{
		$.ajax({
	        url: __root + "/validBgrRuleExp.do",
	        type: "POST",
	        data: {
	        	expText: expText,
	        	upgrade: true
	        },
	        async: false,
	        success: function (text) {
	        	if(text == "1"){
	        		expSql = expSql.replace("@@conditions", expText);
	        		bgrUprulesql.setValue(expSql);
	        	}else{
	        		bgrUprulesql.setValue("");
					e.errorText = "填写有效的表达式！";
			        e.isValid = false;
	        	}
	        }
	    });
	}
}

/**
 * 等级升级规则填写说明
 */
function bgrUpRuleHelp(){
	var title = "帮助提示：";
	var html = "<font color='red'>&nbsp;&nbsp;"
			 + "1、填写的表达式基本格式为﹛条件﹜。<br/>&nbsp;&nbsp;" 
			 + "2、表达式中允许出现的符号：</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "①、算数运算符 +、-、*、/、%</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "②、关系运算符 >、<、>=、<=、=、!=<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "③、逻辑运算符 ||、&&<br/>&nbsp;&nbsp;" 
			 + "3、已提供的系统变量：@buys_total (累积购买商品总额)<br/>&nbsp;&nbsp;" 
			 + "4、举例：@buys_total >= 100"
			 + "</font>";
	
	mini.showMessageBox({
		width: 420,
		height: 240,
        title: title,
        html: html,
        buttons: ["ok"],
        callback: null
    });
}

/**
 * 等级升级规则sql语句
 */
function bgrUpRuleSql(){
	var value = mini.get("bgrUprulesql").getValue();
	mini.alert("升级规则SQL：<br/>" + value);
}

/**
 * 等级降级规则表达式验证过程
 * @param {} e
 */
function validBgrDownRule(e){
	var bgrDownrulesql = mini.get("bgrDownrulesql");
	var expSql = "select (case when @@conditions then 1 end) into @exe_result";
	var expText = e.value.trim();
	
	if(/^$/.test(expText)){
		bgrDownrulesql.setValue("");
		e.errorText = "请填写降级规则表达式！";
        e.isValid = false;
	}else{
		$.ajax({
	        url: __root + "/validBgrRuleExp.do",
	        type: "POST",
	        data: {
	        	expText: expText,
	        	upgrade: false
	        },
	        async: false,
	        success: function (text) {
	        	if(text == "1"){
	        		expSql = expSql.replace("@@conditions", expText);
	        		bgrDownrulesql.setValue(expSql);
	        	}else{
	        		bgrDownrulesql.setValue("");
					e.errorText = "填写有效的表达式！";
			        e.isValid = false;
	        	}
	        }
	    });
	}
}

/**
 * 等级降级规则填写说明
 */
function bgrDownRuleHelp(){
	var title = "帮助提示：";
	var html = "<font color='red'>&nbsp;&nbsp;"
			 + "1、填写的表达式基本格式为﹛条件﹜。<br/>&nbsp;&nbsp;" 
			 + "2、表达式中允许出现的符号：</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "①、算数运算符 +、-、*、/、%</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "②、关系运算符 >、<、>=、<=、=、!=<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "③、逻辑运算符 ||、&&<br/>&nbsp;&nbsp;" 
			 + "3、已提供的系统变量：@buys_new (单位时间内新增消费总额)<br/>&nbsp;&nbsp;" 
			 + "4、举例：@buys_new < 100"
			 + "</font>";
	
	mini.showMessageBox({
		width: 420,
		height: 240,
        title: title,
        html: html,
        buttons: ["ok"],
        callback: null
    });
}

/**
 * 等级降级规则sql语句
 */
function bgrDwonRuleSql(){
	var value = mini.get("bgrDownrulesql").getValue();
	mini.alert("降级规则SQL：<br/>" + value);
}